package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.Action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTConfigService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblThongTinTKB;
import com.lb.jwitter.service.TblThongTinTKBService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class ReportNhomLopMonHocAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}
	
	private Long id; 
    public Long getId() {
       return id;
 }

    public void setId(Long id) {
       this.id = id;
 }
    private String ma; 
    public void setMa(String ma) {
       this.ma = ma;
   }

    public String getMa() {
       return ma;
   }

    private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
  }

    public int getDotDKId() {
       return dotDKId;
  }

    private String maMonHoc;
    public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}
	private Message msg; 
    public Message getMsg() { return msg; }
    
    private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
	
	private Collection<TblThongTinNhomLopMonHoc> tblThongTinNhomLopMonHocs;

	public Collection<TblThongTinNhomLopMonHoc> getTblThongTinNhomLopMonHocs() {
		return tblThongTinNhomLopMonHocs;
	}

	private TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService;

	private Collection<TblDKMHDotDK> tblDKMHDotDKs;

	public Collection<TblDKMHDotDK> getTblDKMHDotDKs() {
		return tblDKMHDotDKs;
	}

	private TblDKMHDotDKService tblDKMHDotDKService;

	private Collection<TblCTDTMonHoc> tblCTDTMonHocs;

	public Collection<TblCTDTMonHoc> getTblCTDTMonHocs() {
		return tblCTDTMonHocs;
	}

	private TblCTDTMonHocService tblCTDTMonHocService;

	private Collection<TblThongTinTKB> tblThongTinTKBs;

	public Collection<TblThongTinTKB> getTblThongTinTKBs() {
		return tblThongTinTKBs;
	}

	private TblThongTinTKBService tblThongTinTKBService;

	private Collection<TblCanBo> tblCanBos;

	public Collection<TblCanBo> getTblCanBos() {
		return tblCanBos;
	}

	private TblCanBoService tblCanBoService;

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String form() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_NhomLop_MonHoc","xem")) {
				return "noprivilege";
			}
			tblThongTinNhomLopMonHocService = (TblThongTinNhomLopMonHocService) context.getBean("tbl_ThongTin_NhomLop_MonHocClient");
			tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");
			//tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient");
			//tblThongTinTKBService = (TblThongTinTKBService) context.getBean("tbl_ThongTin_TKBClient");
			
			tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient");

			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			
			//tblThongTinNhomLopMonHocs = tblThongTinNhomLopMonHocService.findAlltblThongTinNhomLopMonHoc(pageSize, page);
			//pages = tblThongTinNhomLopMonHocService.counttblThongTinNhomLopMonHocByCondition(new ArrayList<ConditionReport>())/ pageSize + 1;
			//dataString = "";
			//dataString += "&ma=";
			//dataString += "&dotDKId=";
			//dataString += "&monHocId=";
			
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			{ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("TKBId.soHVDangKy");
		        con.setValue1(0);
		        con.setOperator("eq");
		        cons.add(con);	
		    }
			tblThongTinNhomLopMonHocs = tblThongTinNhomLopMonHocService.findtblThongTinNhomLopMonHocByCondition(cons, pageSize, page);
			pages = tblThongTinNhomLopMonHocService.counttblThongTinNhomLopMonHocByCondition(cons)
			/ pageSize + 1;
			
			tblDKMHDotDKs = tblDKMHDotDKService.findAlltblDKMHDotDK(0, 0);
			//tblCTDTMonHocs = tblCTDTMonHocService.findAlltblCTDTMonHoc(0, 0);
			//tblThongTinTKBs = tblThongTinTKBService.findAlltblThongTinTKB(0, 0);
			
			tblCanBos = tblCanBoService.findAlltblCanBo(0, 0);
			
			
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Form tbl_ThongTin_NhomLop_MonHoc Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	public String search() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CTDT_Config", "xem")) {
				return "noprivilege";
			}
			tblThongTinNhomLopMonHocService = (TblThongTinNhomLopMonHocService) context.getBean("tbl_ThongTin_NhomLop_MonHocClient");
			msg = new Message();
			List<ConditionReport> cons = new ArrayList<ConditionReport>();

			if(ma != null && !ma.equalsIgnoreCase("")){ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("ma");
		        con.setValue1("%" + ma + "%");
		        con.setOperator("like");
		        cons.add(con);	
		    } 
		    if(dotDKId != 0 ){ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("dotDKId.id");
		        con.setValue1(new Long(dotDKId));
		        con.setOperator("eq");
		        cons.add(con);	
		    } 
		    if(maMonHoc != null && !maMonHoc.equals("") ){ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("monHocId.ma");
		        con.setValue1("%" + maMonHoc + "%");
		        con.setOperator("like");
		        cons.add(con);	
		    } 
		    
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("TKBId.soHVDangKy");
		        con.setValue1(0);
		        con.setOperator("eq");
		        cons.add(con);	
		    } 
			
			preSearch(msg, cons);
			if (msg.isPreMessage_b()) {
				if (pageSize == 0) {
					pageSize = 20;
				}
				if (page == 0) {
					page = 1;
				}
				tblThongTinNhomLopMonHocs = tblThongTinNhomLopMonHocService.findtblThongTinNhomLopMonHocByCondition(cons, pageSize, page);
				pages = tblThongTinNhomLopMonHocService.counttblThongTinNhomLopMonHocByCondition(cons)
						/ pageSize + 1;
				postSearch(msg, cons);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			dataString = ""; 
		      dataString += "&ma=" + ma; 
		      dataString += "&dotDKId=" + dotDKId; 
		      dataString += "&maMonHoc=" + maMonHoc; 
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Seach report nhom lopmonhoc Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

}
